package cn.edu.nju.software.cdiy.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import cn.edu.nju.software.cdiy.bean.Value;
import cn.edu.nju.software.cdiy.dao.ValueDao;

public class ValueDaoImpl implements ValueDao {

	private Map<Integer,Value> keyMap = new HashMap<Integer,Value>();
	private Map<String,Value> valueMap = new HashMap<String,Value>();
	private List<Value> priceList=new ArrayList<Value>();
	private List<Value> typeList=new ArrayList<Value>();
	private List<Value> brandList=new ArrayList<Value>();
	@Override
	public Value getValue(int key) {
		return keyMap.get(key);
	}

	@Override
	public Value getValue(String value) {
		return valueMap.get(value);
	}

	public ValueDaoImpl(SessionFactory sessionFactory) {
		Session session = sessionFactory.openSession();
		try {
			@SuppressWarnings("unchecked")
			List<Value> valueList = session.createQuery("from Value v where v.key<3020 and v.key>1999").list();
			for (Value v : valueList) {
				valueMap.put(v.getValue(), v);
				keyMap.put(v.getKey(), v);
			}
	 priceList=session.createQuery("from Value v where v.key>=3021 and v.key<=3030 order by v.key ").list();
	 typeList=session.createQuery("from Value v where v.key>=2081 and v.key<=2999 order by v.key").list();
	 brandList=session.createQuery("from Value v where v.key>=3031 and v.key<3040 order by v.key").list();
		} catch(Exception e) {
			e.printStackTrace();
		} finally{
			if(null!=session)
				session.close();
		}
	}

	public List<Value> getPriceList() {
		return priceList;
	}

	public void setPriceList(List<Value> priceList) {
		this.priceList = priceList;
	}

	public List<Value> getTypeList() {
		return typeList;
	}

	public void setTypeList(List<Value> typeList) {
		this.typeList = typeList;
	}

	public List<Value> getBrandList() {
		return brandList;
	}

	public void setBrandList(List<Value> brandList) {
		this.brandList = brandList;
	}


}
